package com.example.mdoncall;

import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author Anthony Maniquiz & Laine Nicolas (Smart Telecom Inc. OJT) 
 * @version 1
 * 
 */

public class PatientInfoActivity extends Activity{
	
	String dateOfbirth;
	
	@Override
	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
		setContentView(R.layout.activity_patientinfo);
		setOrientation();
		
		Intent intent = getIntent();
		String strNumber = intent.getStringExtra("callerNumber");
		
		if(strNumber != null){
		
		EditText contactNumber = (EditText) findViewById(R.id.contactField);
		contactNumber.setText(strNumber);
		
		}
		
        Button dateBtn = (Button) findViewById(R.id.datebtn);
        
        final OnDateSetListener odsl=new OnDateSetListener(){
            public void onDateSet(DatePicker arg0, int year, int month, int dayOfMonth) {
             // TODO Auto-generated method stub
         	   String fMonth = null;
         	   String nMonth = null;
         	   switch(month){
         	   case 0: 	fMonth = "Jan";
         	   			nMonth = "01";
         	   			break;
         	   case 1: 	fMonth = "Feb";
         	   			nMonth = "02";
 	   					break;
         	   case 2:	fMonth = "Mar";
         	   			nMonth = "03";
 	   					break;
         	   case 3: 	fMonth = "Apr";
         	   			nMonth = "04";
 	   					break;
         	   case 4: 	fMonth = "May";
         	   			nMonth = "05";
 	   					break;
         	   case 5: 	fMonth = "Jun";
         	   			nMonth = "06";
 	   					break;
         	   case 6:	fMonth = "Jul";
         	   			nMonth = "07";
 	   					break;
         	   case 7:	fMonth = "Aug";
         	   			nMonth = "08";
 	   					break;
         	   case 8:	fMonth = "Sep";
   						nMonth = "09";
 	   					break;
         	   case 9: 	fMonth = "Oct";
         	   			nMonth = "10";
 	   					break;
         	   case 10: fMonth = "Nov";
         	   			nMonth = "11";
 	   					break;
         	   case 11: fMonth = "Dec";
         	   			nMonth = "12";
 	   					break;
         	   }
         	   
             TextView txt=(TextView)findViewById(R.id.bdayField);
             txt.setText(dayOfMonth+" "+fMonth+" "+year);
             dateOfbirth = year +"-"+ nMonth +"-"+ dayOfMonth;
            }};

         dateBtn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
             // TODO Auto-generated method stub
             Calendar cal=Calendar.getInstance();
             DatePickerDialog datePickDiag=new DatePickerDialog(PatientInfoActivity.this,odsl,cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH));
             datePickDiag.show();
            }});
    }
	
	
	public void goNext(View view){
		PersonDbHelper db = new PersonDbHelper(this);
		List<Contact> contacts = db.getAllContacts();
		Intent intent = new Intent(this, OtherPatientInfoActivity.class);
		
		RadioGroup radioGroupGender;
		RadioButton radioButtonGender;
		
		radioGroupGender = (RadioGroup) findViewById(R.id.radioGroup1);
		int selectHereditary = radioGroupGender.getCheckedRadioButtonId();
		radioButtonGender = (RadioButton) findViewById(selectHereditary);
		
		EditText familyName = (EditText) findViewById(R.id.lastnameField);
		EditText firstName = (EditText) findViewById(R.id.firstnameField);
		EditText middleName = (EditText) findViewById(R.id.middlenameField);
		EditText bDay = (EditText) findViewById(R.id.bdayField);
		EditText contactNumber = (EditText) findViewById(R.id.contactField);
		
		EditText street = (EditText) findViewById(R.id.streetField);
		EditText city = (EditText) findViewById(R.id.cityField);
		
		
		if ( familyName.getText().toString().length() == 0 || firstName.getText().toString().length() == 0 ) {
			Toast.makeText(getApplicationContext(), "Please fill up First and Last Name", Toast.LENGTH_SHORT).show();
		}else if(familyName.getText().toString().length() != 0 && firstName.getText().toString().length() != 0){
			Boolean isDuplicated = false;
			
			for(Contact cn: contacts){
				if(familyName.getText().toString().equalsIgnoreCase(cn.getLname()) && firstName.getText().toString().equalsIgnoreCase(cn.getFname()) && middleName.getText().toString().equalsIgnoreCase(cn.getMname())){
					Toast.makeText(getApplicationContext(), familyName.getText().toString() + ", " + firstName.getText().toString() + " " + middleName.getText() + " is already registered.", Toast.LENGTH_SHORT).show();
					isDuplicated = true;
				}
			}
			
			if(isDuplicated == false){
				String[] info = { familyName.getText().toString() , firstName.getText().toString() , middleName.getText().toString() , 
						radioButtonGender.getText().toString() , bDay.getText().toString(), contactNumber.getText().toString(),
						street.getText().toString(), city.getText().toString() };
	
				intent.putExtra("info",info);
				startActivity(intent);
			}
		} 
		
		
		
	}
	
	protected void setOrientation() {
    	int current = getRequestedOrientation();
    	if(current != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
    		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    	}
    }

}
